package org.bonorsoft.modules.system.controller;

import org.apache.shiro.SecurityUtils;
import org.bonorsoft.common.config.Global;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.TokenUtils;
import org.bonorsoft.modules.user.service.ISysDepartService;
import org.bonorsoft.modules.system.entity.SysMenu;
import org.bonorsoft.modules.system.service.ISysConfigService;
import org.bonorsoft.modules.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/** 功能描述:首页  业务处理
 *
 * @vison: 1.0.0
 * @Author: WilliamYe
 * @Date: 2020/11/17 16:31
 */
@Controller
public class SysIndexController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDepartService sysDepartService;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap, HttpServletRequest request)
    {

        // 取身份信息
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        //获取当前登陆部门信息
        //SysDepart depart = sysDepartService.getDepartNameById(user.getDeptId());
        String token = TokenUtils.getTokenByRequest(request);
        System.out.println(menus);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("token", token);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", Global.getCopyrightYear());
        /*mmap.put("demoEnabled", Global.isDemoEnabled());*/
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap)
    {
        return "skin";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        mmap.put("version", Global.getVersion());
        return "main_v1";
    }
}
